/*******************************************************************************************************************************
	
	 Agent Maintainer
	 
*******************************************************************************************************************************/	

/*******************************************************************************************************************************
	 GOALS TRIGGERED
*******************************************************************************************************************************/	
	
/*******************************************************************************************************************************
	RegisterAndMatchRequest:  Registers a request in the environment requests database and sends matching offers back.
	----------------------------------------------------------------------------------------------------------------------
	Parameters:
	Item 		: The item to registers.
*******************************************************************************************************************************/
+!registerAndMatchRequest(Item)[source(Buyer)] : true <-
	.print("Registering and matching request for item ", Item);
	registerRequest(Item,Buyer); /* Environment action, writes request in database.*/
	findMatchingOffers(Item,Buyer); /* Environment action, searches for matching offers.*/
	?matchingOffers(Buyer,Item,Sellers)[source(percept)];
	.send(Buyer,tell,registerAndMatchRequest(Item,Sellers,succeeded)). /* Inform trader request is succesfully  registered.*/

-!registerAndMatchRequest(Item)[source(Buyer)] : true <-
	.send(Buyer, tell, registerAndMatchRequest(Item,[],failed)). /* Inform trader request failed to register.*/

/*******************************************************************************************************************************
	RegisterOffer: Registers an offer in the environment offers database and sends result back. 
	----------------------------------------------------------------------------------------------------------------------
	Parameters:
	Item 		: The item to registers.
*******************************************************************************************************************************/
+!registerOffer(Item)[source(Seller)] : true <-
	.print("Registering Offer for item ", Item);
     registerOffer(Item,Seller); /* Environment action, writes offers in database, and puts the buyers which requests the item in percept.*/
	 .send(Seller, tell, registerOffer(Item,succeeded)). /* Inform trader offer is succesfully registered.*/
	 
-!registerOffer(Item)[source(Seller)] : true <-
	.send(Seller,tell,registerOffer(Item,failed)). /* Inform trader offer failed to register.*/

/*******************************************************************************************************************************
	RemoveRequest:	Removes a registered request from the environment requests database and sends result back. 
	----------------------------------------------------------------------------------------------------------------------
	Parameters:
	Item 		: The item to registers.
*******************************************************************************************************************************/
+!removeRequest(Item)[source(Buyer)] : true <-
	.print("Removing Request for item ",Item);
	removeRequest(Item,Buyer); /*Environment action, removes the registered request*/
	.send(Buyer,tell,removeRequest(Item,succeeded)).

-!removeRequest(Item)[source(Buyer)] : true <-
	.send(Buyer,tell,removeRequest(Item,failed)).
	
/*******************************************************************************************************************************
	Removeoffer: Removes a registered offer from the environment offers database and sends result back. 
	----------------------------------------------------------------------------------------------------------------------
	Parameters:
	Item:	 The item to registers.
*******************************************************************************************************************************/
+!removeOffer(Item)[source(Seller)] : true <-
	.print("Removing Offer for item ", Item);
	 removeOffer(Item,Seller); /*Environment action, removes the registered offer*/
	.send(Seller,tell,removeOffer(Item,succeeded)).
	
-!removeOffer(Item)[source(Seller)] : true <-
	.send(Seller,tell,removeOffer(Item,failed)).

/*******************************************************************************************************************************
	 PERCEPTS
*******************************************************************************************************************************/
	
/*******************************************************************************************************************************
	BuyersWhereOfferAdded:	A percept given in response to a registerOffer, the buyers matching the given offer. 
	----------------------------------------------------------------------------------------------------------------------
	Parameters:
	Seller:	The seller of the offer.
	Item: The item that is newly offered.
	Buyers: The buyers that have to be informed of the new offer.
*******************************************************************************************************************************/
+buyersWhereOfferAdded(Seller,Item,Buyers)[source(percept)] : true <-
	for(.member(Buyer,Buyers))
	{
		.send(Buyer,tell,matchingOfferAdded(Item,Seller));
	}
	-buyersWhereOfferAdded(Seller,Item,Buyers)[source(percept)].

/*******************************************************************************************************************************
	BuyersWhereOfferAdded:	A percept given in response to a removeOffer, the buyers matching the given offer. 
	----------------------------------------------------------------------------------------------------------------------
	Parameters:
	Seller:	The seller of the offer.
	Item: The item that is newly offered.
	Buyers: The buyers that have to be informed of the new offer.
*******************************************************************************************************************************/
+buyersWhereOfferRemoved(Seller,Item,Buyers)[source(percept)] : true <-
	for(.member(Buyer,Buyers))
	{
		.send(Buyer,tell,matchingOfferRemoved(Item,Seller));
	}
	-buyersWhereOfferRemoved(Seller,Item,Buyers)[source(percept)].
	
